package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Business;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/consumptionReport")
public class ConsumptionReportController {
    @Autowired
    private IConsumptionReportService consumptionReportService;
    @Autowired
    private IBusinessService businessService;


    @RequestMapping("/list")
    public String list(Model model , @ModelAttribute("qo") ConsumptionReportQueryObject qo){
        PageInfo pageInfo = consumptionReportService.selectForList(qo);
        model.addAttribute("pageInfo",pageInfo);

        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses",businesses);
        return "/consumptionReport/list";
    }

    @RequestMapping("/listBar")
    public String listBar(Model model , @ModelAttribute("qo") ConsumptionReportQueryObject qo){
       List<Map<String,Object>> list = consumptionReportService.selectForChart(qo.getGroupType());

       List<Object> groupType = new ArrayList<>();
       List<Object> totalAmount = new ArrayList();
       List<Object> payAmount = new ArrayList();
       List<Object> discountAmount = new ArrayList();
       List<Object> count = new ArrayList();

        for (Map<String, Object> map : list) {
            groupType.add(map.get("groupType"));
            totalAmount.add(map.get("total_amount"));
            payAmount.add(map.get("pay_amount"));
            discountAmount.add(map.get("discount_amount"));
            count.add(map.get("count"));
        }

        model.addAttribute("groupType",JSON.toJSONString(groupType));
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmount));
        model.addAttribute("payAmount",JSON.toJSONString(payAmount));
        model.addAttribute("discountAmount",JSON.toJSONString(discountAmount));
        model.addAttribute("count",JSON.toJSONString(count));
        return "/consumptionReport/listBar";
    }
}
